home *** CD-ROM | disk | FTP | other *** search
- /* main.c - main file of Balloonify
- November 11, 1996: I, David Phillip Oster, place this source code in the public
- domain. This means you can do anyhing you want with it.
- It would be considerate if you kept me posted on any bugs, bug fixes, or
- improvements.
- oster@netcom.com
- */
- #include "Balloonify.h"
- #include "BalloonyRes.h"
-
- /* kFileMenu - items
- */
- enum {
- kOpenI = 1,
- kQuitI = kOpenI + kSkipGrayLine
- };
-
- /* kEditMenu - items
- */
- enum {
- kUndoI = 1,
- kCutI = kUndoI + kSkipGrayLine,
- kCopyI,
- kPasteI,
- kClearI
- };
-
- #define bDoOpenCommandOnce (1L << 0)
- #define bDone (1L << 1)
-
- /* *** globals
- */
- StringPtr emptyS = "\p";
-
- /* forward declarations.
- */
- static void HandleEvents(void);
-
- /* *** local to this file.
- */
- static LongInt localEventSelect = 0;
- static DialogPtr progressDlog = NIL;
- static OSErr progressErrCode = noErr;
-
- /* *** error routines.
- */
-
- /* Error strings
- */
- enum{
- kGenericErrS = 1,
- kDirFulErr, /*Directory full*/
- kDskFulErr, /*disk full*/
- kNsvErr, /*no such volume*/
- kIOErr, /*I/O error*/
- kBdNamErr, /*there may be no bad names in the final system!*/
- kFnOpnErr, /*File not open*/
- kEofErr, /*End of file*/
- kPosErr, /*tried to position to before start of file (r/w)*/
- kMFulErr, /*memory full (open) or file won't fit (load)*/
- kTmfoErr, /*too many files open*/
- kFnfErr, /*File not found*/
- kWPrErr, /*diskette is write protected.*/
- kFLckdErr, /*file is locked*/
- kVLckdErr, /*volume is locked*/
- kFBsyErr, /*File is busy (delete)*/
- kDupFNErr, /*duplicate filename (rename)*/
- kOpWrErr, /*file already open with with write permission*/
- kRfNumErr, /*refnum error*/
- kGfpErr, /*get file position error*/
- kVolOffLinErr, /*volume not on line error (was Ejected)*/
- kPermErr, /*permissions error (on file open)*/
- kVolOnLinErr, /*drive volume already on-line at MountVol*/
- kNsDrvErr, /*no such drive (tried to mount a bad drive num)*/
- kNoMacDskErr, /*not a mac diskette (sig bytes are wrong)*/
- kExtFSErr, /*volume in question belongs to an external fs*/
- kFsRnErr, /*file system internal error:during rename the old entry was deleted but could not be restored.*/
- kBadMDBErr, /*bad master directory block*/
- kWrPermErr, /*write permissions error*/
- kDirNFErr, /*Directory not found*/
- kTmwdoErr, /*No free WDCB available*/
- kBadMovErr, /*Move into offspring error*/
- kWrgVolTypErr, /*Wrong volume type error [operation not supported for MFS]*/
- kVolGoneErr, /*Server volume has been disconnected.*/
- kMemFullError /*Insufficent memory to complete operation.*/
- };
-
- /* GetErrorStringCode - translate system error code to string index
- */
- static Integer GetErrorStringCode(OSErr errCode){
- switch(errCode){
- case noErr:
- case eUserCancel: return 0;
- case dirFulErr: return kDirFulErr; /*Directory full*/
- case dskFulErr: return kDskFulErr; /*disk full*/
- case nsvErr: return kNsvErr; /*no such volume*/
- case ioErr: return kIOErr; /*I/O error*/
- case bdNamErr: return kBdNamErr; /*there may be no bad names in the final system!*/
- case fnOpnErr: return kFnOpnErr; /*File not open*/
- case eofErr: return kEofErr; /*End of file*/
- case posErr: return kPosErr; /*tried to position to before start of file (r/w)*/
- case mFulErr: return kMFulErr; /*memory full (open) or file won't fit (load)*/
- case tmfoErr: return kTmfoErr; /*too many files open*/
- case fnfErr: return kFnfErr; /*File not found*/
- case wPrErr: return kWPrErr; /*diskette is write protected.*/
- case fLckdErr: return kFLckdErr; /*file is locked*/
- case vLckdErr: return kVLckdErr; /*volume is locked*/
- case fBsyErr: return kFBsyErr; /*File is busy (delete)*/
- case dupFNErr: return kDupFNErr; /*duplicate filename (rename)*/
- case opWrErr: return kOpWrErr; /*file already open with with write permission*/
- case rfNumErr: return kRfNumErr; /*refnum error*/
- case gfpErr: return kGfpErr; /*get file position error*/
- case volOffLinErr: return kVolOffLinErr; /*volume not on line error (was Ejected)*/
- case permErr: return kPermErr; /*permissions error (on file open)*/
- case volOnLinErr: return kVolOnLinErr; /*drive volume already on-line at MountVol*/
- case nsDrvErr: return kNsDrvErr; /*no such drive (tried to mount a bad drive num)*/
- case noMacDskErr: return kNoMacDskErr; /*not a mac diskette (sig bytes are wrong)*/
- case extFSErr: return kExtFSErr; /*volume in question belongs to an external fs*/
- case fsRnErr: return kFsRnErr; /*file system internal error:during rename the old entry was deleted but could not be restored.*/
- case badMDBErr: return kBadMDBErr; /*bad master directory block*/
- case wrPermErr: return kWrPermErr; /*write permissions error*/
- case dirNFErr: return kDirNFErr; /*Directory not found*/
- case tmwdoErr: return kTmwdoErr; /*No free WDCB available*/
- case badMovErr: return kBadMovErr; /*Move into offspring error*/
- case wrgVolTypErr: return kWrgVolTypErr; /*Wrong volume type error [operation not supported for MFS]*/
- case volGoneErr: return kVolGoneErr; /*Server volume has been disconnected.*/
- case memFullErr: return kMemFullError;
- default: return kGenericErrS;
- }
- }
-
- /* TellError - if an error occurred, report it to the user.
- */
- static OSErr TellError(OSErr errCode){
- Str255 errS, s;
- Integer errN;
-
- if(0 == (errN = GetErrorStringCode(errCode))){
- return errCode;
- }
- GetIndString(errS, kErrorStrs, errN);
- NumToString(errCode, s);
- ParamText(errS, s, emptyS, emptyS);
- Alert(rError, NIL);
- return errCode;
- }
-
- /* *** progress routines.
- */
-
- /* StartProgress - show the progress dialog.
- */
- void StartProgress(void){
- if(NIL == progressDlog){
- progressErrCode = noErr;
- progressDlog = GetNewDialog(rProgress, NIL, (WindowPtr) -1L);
- ShowWindow(progressDlog);
- SelectWindow(progressDlog);
- SetPort(progressDlog);
- DrawDialog(progressDlog);
- ValidRect(&progressDlog->portRect);
- }
- }
-
-
- /* StopProgress - get rid of the prgress dialog.
- */
- void StopProgress(void){
- if(NIL != progressDlog){
- DisposeWindow(progressDlog);
- progressDlog = NIL;
- }
- }
-
- #define kProgressI 3
-
- /* BumpProgress - show the progress dialog
- */
- OSErr BumpProgress(StringPtr this){
- Integer type;
- Handle h;
- Rect r;
-
- if(NIL != progressDlog){
- GetDItem(progressDlog, kProgressI, &type, &h, &r);
- SetIText(h, this);
- HandleEvents();
- }
- return progressErrCode;
- }
-
-
- /* *** functions.
- */
- Integer Min(Integer a, Integer b){ return a < b ? a : b; }
-
- /* *** string functions.
- */
-
- /* StrMove -
- */
- void StrMove(const StringPtr src, StringPtr dest){
- BlockMove(src, dest, Length(src) + 1);
- }
-
- /* AppendChar - safe add char to string
- */
- void AppendChar(StringPtr s, char c){
- if(Length(s) < 254){
- s[0] = 1 + Length(s);
- s[Length(s)] = c;
- }
- }
-
- /* Concat - stick the tail on the string
- */
- void Concat(StringPtr s, StringPtr tail){
- Integer newLen;
-
- newLen = Min(255, Length(s) + Length(tail));
- BlockMove((Ptr) &tail[1] ,(Ptr) &s[1+Length(s)], newLen - Length(s));
- s[0] = newLen;
- }
-
-
- /* NullUpdate - the empty update routine.
- */
- static pascal void NullUpdate(DialogPtr dp, Integer i){
- }
-
-
- /* SetWDIHandle -
- */
- static void SetWDIHandle(WindowPtr win, Integer item, Handle h){
- Integer theType;
- Handle theHandle;
- Rect theRect;
-
- GetDItem(win, item, &theType, &theHandle, &theRect);
- SetDItem(win, item, theType, h, &theRect);
- }
-
- /* SetDIHandle -
- */
- static void SetDIHandle(Integer item, Handle h){
- SetWDIHandle(qd.thePort, item, h);
- }
-
- /* GetAboutWin -
- */
- static WindowPtr GetAboutWin(void){
- WindowPtr win;
- DialogTHndl dt;
-
- if(NIL == (dt = (DialogTHndl) GetResource('DLOG', rAbout))){
- return;
- }
- HNoPurge((Handle) dt);
- win = GetNewDialog(rAbout, NIL, (WindowPtr) -1L);
- SetDIHandle(1, (Handle) NullUpdate);
- ShowWindow(win);
- HPurge((Handle) dt);
- return win;
- }
-
- /* DoAppleMenu -
- */
- static void DoAppleMenu(Integer item){
- Str255 s;
- WindowPtr win;
-
- if(1 == item){
- win = GetAboutWin();
- ModalDialog(NIL, &item);
- DisposeWindow(win);
- }else{
- GetItem(GetMHandle(kAppleMenu), item, s);
- OpenDeskAcc(s);
- }
- }
-
- /* DoOpen -
- */
- static void DoOpen(void){
- StandardFileReply reply;
- static OSType applType[] = { 'APPL', 'rsrc', 'RSRC', 'dfil' };
-
- StandardGetFile(NIL, sizeof(applType)/sizeof(OSType), applType, &reply);
- if(reply.sfGood){
- TellError(Balloonify(&reply.sfFile, reply.sfScript));
- }
- }
-
- /* DoQuit - do a quit command
- */
- static void DoQuit(void){
- ExitToShell();
- }
-
- /* DoFileMenu -
- */
- static void DoFileMenu(Integer item){
- switch(item){
- case kOpenI: DoOpen(); break;
- case kQuitI: DoQuit(); break;
- }
- }
-
- /* DoEditMenu -
- */
- static void DoEditMenu(Integer item){
- if(NOT SystemEdit(item - 1)){
- switch(item){
- case kUndoI:
- case kCutI:
- case kCopyI:
- case kPasteI:
- case kClearI:
- break;
- }
- }
- }
-
- /* GoMenu - do one menu command
- */
- static void GoMenu(LongInt selector){
- switch(HiWord(selector)){
- case kAppleMenu: DoAppleMenu(LoWord(selector)); break;
- case kFileMenu: DoFileMenu(LoWord(selector)); break;
- case kEditMenu: DoEditMenu(LoWord(selector)); break;
- }
- HiliteMenu(0);
- }
-
- /* FSSpecFunc - call this with an FSSpecPtr and return an OSErr
- */
- typedef OSErr (*FSSpecFunc)(FSSpecPtr);
-
- /* BetaExpired - TRUE is later than August 1, 1994
- >>>
- */
- static Boolean BetaExpired(void){
- return FALSE;
- }
-
- static void InitPreferences(void){
- }
-
- /* MissedAEParameters -
- */
- static OSErr MissedAEParameters(AppleEvent *message){
- DescType typeCode;
- Size actualSize;
- OSErr err;
-
- if(errAEDescNotFound == (err = AEGetAttributePtr(message, keyMissedKeywordAttr, typeWildCard,
- &typeCode, NIL, 0L, &actualSize))){
-
- return noErr;
- }
- return (noErr == err ? errAEEventNotHandled : err);
- }
-
- /* FSOpenWD - convenience function for opening a wRef
- */
- static OSErr FSOpenWD(Integer vRef, LongInt dirId, OSType signature, Integer *wRef){
- WDPBRec io;
- OSErr val;
-
- io.ioNamePtr = NIL;
- io.ioVRefNum = vRef;
- io.ioWDDirID = dirId;
- io.ioWDProcID = signature;
- if(noErr == (val = PBOpenWD(&io, FALSE))){
- *wRef = io.ioVRefNum;
- }
- return val;
- }
-
- /* DirIDVRefToWRef - given a dirId, vRef pair, return the working ref
- */
- static OSErr DirIDVRefToWRef(Integer vRef, LongInt dirID, Integer *wRefp){
- return FSOpenWD(vRef, dirID, 'ERIK', wRefp);
- }
-
-
- static OSErr OpenPrint(AppleEvent *message, AppleEvent *reply, long refCon, FSSpecFunc f){
- FSSpec fss;
- AEDescList docList;
- long index, itemsInList;
- Size actualSize;
- AEKeyword keywd;
- DescType typeCode;
- OSErr err;
- SFReply sfr;
- FInfo finfo;
-
- if((err = AEGetParamDesc(message, keyDirectObject, typeAEList, &docList)) != noErr ||
- (err = MissedAEParameters(message)) != noErr ||
- (err = AECountItems(&docList, &itemsInList)) != noErr){
- return err;
- }
-
- for(index = 1; index <= itemsInList; index++){
- if(noErr != (err = AEGetNthPtr(&docList, index, typeFSS, &keywd, &typeCode,
- (Ptr)&fss, sizeof(FSSpec), &actualSize))){
- break;
- }
- if(noErr != (err = (*f)(&fss))){
- break;
- }
- }
- localEventSelect |= bDone;
- return AEDisposeDesc(&docList);
- }
-
- /* DoOpenApp - We get this if we weren't given any documents.
- Nothing to do for now in Balloony
- */
- static pascal OSErr DoOpenApp(AppleEvent *message, AppleEvent *reply, long refCon){
- OSErr err;
-
- if ((err = MissedAEParameters(message)) != noErr){
- return err;
- }
- localEventSelect |= bDoOpenCommandOnce;
- return noErr;
- }
-
- /* Balloonify1 - wrapper for balloonify when we don't know the script code.
- */
- static OSErr Balloonify1(FSSpecPtr fs){
- return TellError(Balloonify(fs, smSystemScript));
- }
-
- /* DoOpenDoc
- */
- static pascal OSErr DoOpenDoc(AppleEvent *message, AppleEvent *reply, long refCon){
- return OpenPrint(message, reply, refCon, Balloonify1);
- }
-
- /* DoPrintDoc
- */
- static pascal OSErr DoPrintDoc(AppleEvent *message, AppleEvent *reply, long refCon){
- return OpenPrint(message, reply, refCon, Balloonify1);
- }
-
- /* DoQuitApp -
- */
- static pascal OSErr DoQuitApp(AppleEvent *message, AppleEvent *reply, long refcon){
- OSErr err;
-
- if ((err = MissedAEParameters(message)) != noErr){
- return err;
- }
- localEventSelect |= bDone;
- return noErr;
- }
-
- static void InitAppleEventHandlers(void){
- LongInt response;
-
- if(noErr == Gestalt(gestaltAppleEventsAttr, &response) &&
- (response & (1L << gestaltAppleEventsPresent))){
-
- AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
- NewAEEventHandlerProc(DoOpenApp), 0, FALSE);
- AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
- NewAEEventHandlerProc(DoOpenDoc), 0, FALSE);
- AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
- NewAEEventHandlerProc(DoPrintDoc), 0, FALSE);
- AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
- NewAEEventHandlerProc(DoQuitApp), 0, FALSE);
- }
- }
-
-
- /* Init
- */
- static void Init(void){
- InitGraf((Ptr) &qd.thePort); /* initialize the toolbox */
- InitFonts();
- InitWindows();
- InitMenus();
- FlushEvents(everyEvent,0);
- TEInit();
- InitDialogs(0L);
- InitCursor();
-
- if(BetaExpired()){
- Alert(kBetaDie, NIL);
- ExitToShell();
- }
- SetMenuBar(GetNewMBar(kMBAR));
- DrawMenuBar();
- AddResMenu(GetMHandle(kAppleMenu), 'DRVR');
- InitBalloonify();
- InitPreferences();
- InitAppleEventHandlers();
- }
-
- /* GoHighLevelEvent - dispatch to apple event processor
- */
- static void GoHighLevelEvent(EventRecord *theEvent){
- AEProcessAppleEvent(theEvent);
- }
-
-
- /* GoMenuBar -
- */
- static void GoMenuBar(EventRecord *event){
- SetCursor(&qd.arrow);
- GoMenu(MenuSelect(event->where));
- }
-
- /* GoKey - the keystroke handler
- */
- static void GoKey(EventRecord *event){
- WindowPtr win;
-
- if(cmdKey & event->modifiers){
- GoMenu(MenuKey((char) event->message));
- }
- }
-
- /* GoMouseDown - the mouse down tracker
- */
- static void GoMouseDown(EventRecord *event){
- Integer sel;
- WindowPtr win;
-
- sel = FindWindow(event->where, &win);
- switch(sel){
- case inMenuBar: GoMenuBar(event); break;
- case inGoAway: /* GoAway(event, win); */ break;
- case inDrag: /* GoDrag(event, win); */ break;
- case inGrow: /* GoGrow(event, win); */ break;
- case inContent: /* GoContent(event, win);*/ break;
- case inSysWindow: SystemClick(event, win); break;
- }
- }
-
- /* GoIdle -
- */
- static void GoIdle(EventRecord *e){
- }
-
- /* GoEvent - main event dispatcher.
- */
- static void GoEvent(EventRecord *e){
- DialogPtr dp;
- Integer item;
-
- if(IsDialogEvent(e) && DialogSelect(e, &dp, &item) &&
- NIL != dp && progressDlog == dp && Cancel == item){
-
- progressErrCode = eUserCancel;
- return;
- }
- switch(e->what){
- case nullEvent: GoIdle(e); break;
- case mouseDown: GoMouseDown(e); break;
- case autoKey:
- case keyDown: GoKey(e); break;
- case updateEvt: break;
- case activateEvt: break;
- case osEvt: break;
- case kHighLevelEvent: GoHighLevelEvent(e); break;
- default: break;
- }
- }
-
- /* HandleEvents -
- */
- static void HandleEvents(void){
- EventRecord e;
-
- if(0 != localEventSelect){
- if(localEventSelect & bDone){
- DoQuit();
- }
- if(localEventSelect & bDoOpenCommandOnce){
- localEventSelect &= ~bDoOpenCommandOnce;
- DoOpen();
- }
- }
- WaitNextEvent(everyEvent, &e, NIL, 0);
- GoEvent(&e); /* allow null event processing */
- }
-
-
- /* main
- */
- main(){
- Init();
- for(;;){
- HandleEvents();
- }
- return 0;
- }
-